package com.itheima.controller;

import com.github.pagehelper.Page;
import com.itheima.pojo.*;
import com.itheima.service.BlogSortService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
@CrossOrigin
@RequestMapping("/blogSort")
public class BlogSortController {


    @Autowired
    private BlogSortService blogSortService;

    //1 增加博客分类  blogSort/add
    @RequestMapping("/add")
    public Result add(@RequestBody BlogSortVO blogSortVO){
        BlogSort blogSort ;
        try {
            blogSort = new BlogSort();
            blogSort.setSortName(blogSortVO.getSortName());
            blogSort.setContent(blogSortVO.getContent());
            blogSort.setSort(blogSortVO.getSort());
            blogSortService.add(blogSort);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(StatusCode.ERROR, "插入失败");
        }
        return new Result(StatusCode.OK, "插入成功");
    }

    // 2 通过引用量排序博客分类 /blogSortByCite
    @RequestMapping("/blogSortByCite")
    public Result blogSortByCite(){
        try {
            blogSortService. blogSortByCite();
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(StatusCode.ERROR, "操作失败");
        }
        return new Result(StatusCode.OK, "操作成功");
    }

    // 3 通过点击量排序博客分类   /blogSort/blogSortByClickCount
    @RequestMapping("/blogSortByClickCount")
    public Result blogSortByClickCount(){
        try {
            blogSortService.blogSortByClickCount();
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(StatusCode.ERROR, "操作失败");
        }
        return new Result(StatusCode.OK, "操作成功");
    }


    // 4 批量删除博客分类   /blogSort/deleteBatch
    @RequestMapping("/deleteBatch")
    public Result deleteBatch(@RequestBody List<BlogSortVO> list){
        //{"code":"error","data":"该分类下还有博客！"}

        Result result;
        try {
            result = blogSortService.deleteBatch(list);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return result;
    }

    // 5 编辑博客分类   POST /blogSort/edit
    @RequestMapping("/edit")
    public Result edit(@RequestBody  BlogSortVO blogSortVO){

        BlogSort blogSort;
        try {
            blogSort = new BlogSort();
            blogSort.setSortName(blogSortVO.getSortName());
            blogSort.setContent(blogSortVO.getContent());
            blogSort.setSort(blogSortVO.getSort());
            blogSort.setStatus(1);
            blogSort.setCreateTime(new Date());
            blogSort.setUpdateTime(new Date());
            blogSort.setUid(blogSortVO.getUid());
            blogSortService.edit(blogSort);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(StatusCode.ERROR, "更新失败");
        }
        return new Result(StatusCode.OK, "更新成功");
    }

    // 6 获取博客分类列表 POST /blogSort/getList
    @RequestMapping("/getList")
    public Map<String, Object> getList(@RequestBody BlogSortVO blogSortVO) throws Exception {
        Page<BlogSort> page = blogSortService.getList(blogSortVO);
        List<BlogSort> list = page.getResult();
        long total = page.getTotal();
        Map<String, Object> map = new HashMap<>();

        ArrayList<Map<String, Object>> blogSortList = new ArrayList<>();
        for (BlogSort blogSort : list) {
            Map<String, Object> map1 = new HashMap<>();
            map1.put("sortName",blogSort.getSortName());
            map1.put("content",blogSort.getContent());
            map1.put("sort",blogSort.getSort());
            map1.put("clickCount",blogSort.getClickCount());
            map1.put("uid",blogSort.getUid());
            map1.put("status",blogSort.getStatus());
            map1.put("createTime", DateUtils.parseDate2String(blogSort.getCreateTime()));
            map1.put("updateTime",DateUtils.parseDate2String(blogSort.getUpdateTime()));
            blogSortList.add(map1);
        }

        map.put("records", blogSortList);
        map.put("total", total);
        map.put("size", blogSortVO.getPageSize());
        map.put("current", blogSortVO.getCurrentPage());
        ArrayList<Object> list2 = new ArrayList<>();
        map.put("orders", list2);
        map.put("optimizeCountSql", true);
        map.put("isSearchCount", true);
        Map<String, Object> result = new HashMap<>();
        result .put("code", "success");
        result .put("data", map);
        return result ;
    }


    // 7  置顶分类  POST /blogSort/stick  //根据sort 值越大越靠前
    @RequestMapping("/stick")
    public Result stick(@RequestBody BlogSortVO blogSortVO){
        //{"code":"success","data":"操作成功"}
        // {"code":"error","data":"该分类已经在顶端！"}
        try {
            blogSortService.stick(blogSortVO);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(StatusCode.ERROR, "该分类已经在顶端");
        }
        return new Result(StatusCode.OK, "操作成功");

    }

}
